package com.daytask.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.daytask.formbean.DaytaskSet;
import com.daytask.formbean.SonTasktype;
import com.daytask.formbean.TasktypeFrom;
import com.daytask.util.ControllorUtils;
import com.daytask.validate.SonTasktypeVal;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class DaytaskSetController extends Controller {
	// id name sontasktypeid sontasktypename caption
	public Map<String, Object> responseM = new HashMap<String, Object>();// 用来存放返回的数据（json）


	//@Before(SonTasktypeVal.class)
	public void add() {
		boolean saveResult = false;
		DaytaskSet form = getModel(DaytaskSet.class);
		if (StringUtils.isNotBlank(form.getStr("name")) && StringUtils.isNotBlank(form.getStr("caption"))
				&&  form.getInt("sontasktypeid")>0) {
			SonTasktype sonTasktype = SonTasktype.dao.findById(getAttr("sontasktypeid"));
			if (sonTasktype != null) {
				form.set("sontasktypeid", sonTasktype.get("id"));
				form.set("sontasktypename", sonTasktype.get("name"));
				saveResult = form.save();
			}
		}
		if (saveResult == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);
	}

	public void delete() {
		String deleteIds = getPara("deleteIds");
		int deleteRecord = 0;
		if (StringUtils.isNotBlank(deleteIds)) {
			String ids[] = StringUtils.split(deleteIds, ",");
			for (String id : ids) {
					if (DaytaskSet.dao.deleteById(id) == true)
						deleteRecord++;
			}
			if (deleteRecord == ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 1);
			} else if (deleteRecord > 0 && deleteRecord < ids.length) {
				ControllorUtils.createJsonResultMap(responseM, 2);// 部分删除成功
			} else {
				ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败
			}
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0); // 删除失败

		}
		renderJson(responseM);
	}

	public void list() {
		int page =Integer.parseInt( getPara("page"));
		page=page<1?1:page;
		int rows = Integer.parseInt(getPara("rows"));
		String select  ="select  a.*,b.quantum   ";
		String fromSql ="from tb_son_tasktype b , tb_daytask_set a where a.sontasktypeid = b.id ";
		Page<TasktypeFrom> list = TasktypeFrom.dao.paginate(page,rows, select, fromSql);
		 responseM.put("total", list.getTotalRow());
		 responseM.put("rows",list.getList());
		  renderJson(responseM);
	}

	public void edit() {
		boolean result = false;
		DaytaskSet form = getModel(DaytaskSet.class);
		int id = form.getInt("id");
		if (StringUtils.isNotBlank(form.getStr("name")) && StringUtils.isNotBlank(form.getStr("caption"))) {
			DaytaskSet dbObj = form.findById(id);
			if (dbObj != null) {
				dbObj.set("name", form.getStr("name")).set("caption", form.getStr("caption"));
				result = dbObj.update();
			}
		}
		if (result == true) {
			ControllorUtils.createJsonResultMap(responseM, 1);
		} else {
			ControllorUtils.createJsonResultMap(responseM, 0);
		}
		renderJson(responseM);

	}

}
